<?php

/*
  Plugin Name: Adwit Banner Manager
  Plugin URI: http://www.adwit-server.com
  Description: Description
  Version: 1.2
  Author: Etineria Inc.
  Author URI: http://www.etineria.com
 */

/*  Copyright 2010  Etineria Inc  (email : info@adwitserver.com)

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License, version 2, as
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
//error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);
  #  if (file_exists(WP_PLUGIN_DIR.'/adwit-dev')) {
    define("ADWIT_HOME_URL", "http://www.adwit-express-.com:3000");
  #}else{
  #  define("ADWIT_HOME_URL", "http://www.adwit-express.com");
  #}
define("ADWIT_BANNER_MANAGER_VERSION", "1.2");
define('ADWIT_BANNER_MANAGER_PATH', WP_PLUGIN_DIR . '/adwit-banner-manager');
define("ADWIT_BANNER_IMAGES", '/wp-content/plugins/adwit-banner-manager/images');
load_plugin_textdomain('adwit', false, 'adwit-banner-manager/lang');

//require_once('/home/dev1/webserver/php/wordpress/wp-content/plugins/logger.class.php');
require_once ADWIT_BANNER_MANAGER_PATH . '/adwit-banner-manager-admin.php';
require_once ADWIT_BANNER_MANAGER_PATH . '/adwit-banner-widget.php';

//Add/Update
if ($_POST && isset($_POST['adwit_action'])) {
    Adwit_Banner_Manager_Admin::adwit_update($_POST);
}

function adwit_widget_options($reset=false) {
    global $widget_adwit_banner_data;
    if (empty($widget_adwit_banner_data) || $reset !== false) {
	$widget_adwit_banner_data = get_option('widget_adwit_banner');
    }
    return $widget_adwit_banner_data;
}

function adwit_options($reset=false) {
    global $adwit_data;
    if (empty($adwit_data) || $reset !== false) {
	$adwit_data = get_option('adwit_options');
    }
    return $adwit_data;
}

function adwit_banner_manager_dashboard() {
    add_object_page('adwit', 'Adwit-Banner', 'manage_options', 'adwit', 'adwit_banner_manager_ads');
    add_submenu_page('adwit', 'adwit > Manage', __('Manage Ads', 'adwit'), 'manage_options', 'adwit', 'adwit_banner_manager_ads');
    add_submenu_page('adwit', 'adwit > Setup Adwit Ads', __('Setup Adwit Ads', 'adwit'), 'manage_options', 'adwit_setup', 'adwit_banner_manager_setup');
}

function adwit_banner_manager_load_scripts() {
    wp_enqueue_script('scriptaculous');
}

function adwit_banner_manager_ads() {
    $res = Adwit_Banner_Manager_Admin::adwit_find_or_create_customer();

    if (is_array($res) && !empty($res)) {
	if ($res['status'] == 0) {
	    global $current_user;
	    get_currentuserinfo();
	    global $adwit_recovery;
	    require_once(ADWIT_BANNER_MANAGER_PATH . '/views/auth.php');
	} elseif ($res['status'] == 2) {
	    echo "<span style=\"color:#ff0000;font-size: 1.2em;\">" . __('You should set at least one zone', 'adwit') . "</span>";
	    adwit_banner_manager_setup(false);
	} else {
	    $adwit_opt = $res['data'];
	    $data = array();
	    $data['date'] = Adwit_Banner_Manager_Admin::_get_date_now_adwit_format();
	    $data['publisher_id'] = $adwit_opt['adwit_express_publisher_id'];

	    echo "<iframe width='100%' height='600' src='" . ADWIT_HOME_URL . '/' . Adwit_Banner_Manager_Admin::adwit_generate_token('campaign_ads/banners', $data, $adwit_opt['adwit_express_key']) . "'></iframe>";
	}
    } else {
	echo "<iframe width='100%' height='600' src='" . ADWIT_HOME_URL . "'></iframe>";
    }
}

function adwit_banner_manager_setup($isnew = true) {
    //Find or create customer
    $do_recovery = false;

    if ($isnew !== false) {
	$res = Adwit_Banner_Manager_Admin::adwit_find_or_create_customer();
	if ($res['status'] == 0) {
	    global $current_user;
	    get_currentuserinfo();
	    global $adwit_recovery;
	    include ADWIT_BANNER_MANAGER_PATH . '/views/auth.php';
	    $do_recovery = true;
	}
    }

    if ($do_recovery == false) {
	$options = adwit_options();
	$vp_type = 'page';

	if (isset($_POST['post_type']) && !empty($_POST['post_type'])) {
	    $vp_type = $_POST['post_type'];
	}

	switch ($vp_type) {
	    case 'post':
		$view_page = 'post';
		$adwit_ads = Adwit_Banner_Manager_Admin::_get_adwit_post_type_data($options['ads'], 'post');
		break;
	    case 'home':
		$view_page = 'home';
		$adwit_ads = Adwit_Banner_Manager_Admin::_get_adwit_post_type_data($options['ads'], 'home');
		break;
	    case 'search':
		$view_page = 'search';
		$adwit_ads = Adwit_Banner_Manager_Admin::_get_adwit_post_type_data($options['ads'], 'search');
		break;
	    default:
		$view_page = 'page';
		$adwit_ads = Adwit_Banner_Manager_Admin::_get_adwit_post_type_data($options['ads'], 'page');
		break;
	}

	include ADWIT_BANNER_MANAGER_PATH . '/views/main.php';
    }
}

function adwit_banner_manager_inject_hook($content) {

    if (is_home() || is_archive() || is_search() || is_page() || is_single() && !empty($content)) {

	$adwit_ads = Adwit_Banner_Manager_Admin::_get_adwit_ads_data();
	if (empty($adwit_ads)) {
	    return $content;
	}

	$paragraphes = preg_match_all("/(<p(.*?)<\/p>)/", $content, $result);

	if (count($result[0]) > 1 and (!is_search() )) {
	    $two_array = array_chunk($result[0], ceil(count($result[0]) / 2));

	    $script_top = Adwit_Banner_Manager_Admin::_get_adwit_ads('top');
	    $two_array[0][0] = $script_top . $two_array[0][0];
	    $script_middle = Adwit_Banner_Manager_Admin::_get_adwit_ads('middle');
	    $script_bottom = Adwit_Banner_Manager_Admin::_get_adwit_ads('bottom');
	    $two_array[1][2] = $two_array[1][2];

	    $res = implode(array_merge($two_array[0], array($script_middle), $two_array[1]));

	    $content = $res . $script_bottom;
	} else {

	    $script_top = Adwit_Banner_Manager_Admin::_get_adwit_ads('top');

	    if (!empty($script_top)) {
		$content = $script_top.$content;
	    }

	    $script_bottom = Adwit_Banner_Manager_Admin::_get_adwit_ads('bottom');
	    if (!empty($script_bottom)) {
		$content .=  $script_bottom;
	    }

	}

    }

    return $content;
}

function adwit_banner_manager_widget_init() {
    register_widget('Widget_Adwit_Banner');
}

add_action('admin_menu', 'adwit_banner_manager_dashboard');
add_action('admin_init', 'adwit_banner_manager_load_scripts');
add_filter('the_content', 'adwit_banner_manager_inject_hook');
add_action('widgets_init', 'adwit_banner_manager_widget_init');

?>